package org.benxiong.iplay.ui.component;

import org.benxiong.iplay.R;
import org.benxiong.iplay.util.MeasurementUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class HeightPreference extends DialogPreference implements OnClickListener, OnCheckedChangeListener{
	private LinearLayout inchLy;
	private EditText cmInput;
	private EditText feetInput;
	private EditText inchInput;
	private ToggleButton tb;
	
	SharedPreferences sp;
	public HeightPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		sp = context.getSharedPreferences("org.benxiong.iplay_preferences", Context.MODE_PRIVATE);
	}

	@Override
	protected void onBindDialogView(View view) {
		// TODO Auto-generated method stub
		super.onBindDialogView(view);
		tb = (ToggleButton) view.findViewById(R.id.toggle);
		tb.setOnClickListener(this);
		tb.setOnCheckedChangeListener(this);
		inchLy = (LinearLayout) view.findViewById(R.id.inch_layout);
		cmInput = (EditText) view.findViewById(R.id.cm_input);
		feetInput = (EditText) view.findViewById(R.id.feet);
		inchInput = (EditText) view.findViewById(R.id.inch);
		
		//bind data and view
		if(sp.getString("measure", "1").equals("0")) {
			tb.setChecked(true);
			cmInput.setText(sp.getString("height", "0"));
		} else {
			int inch = Float.valueOf(sp.getString("inch", "0")).intValue();
			feetInput.setText(""+inch/12);
			inchInput.setText(""+inch%12);
		}
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		// TODO Auto-generated method stub
		super.onDialogClosed(positiveResult);
		if (positiveResult) {
			Editor editor = sp.edit();
			String value = "";
			if (tb.isChecked()) {
				value = cmInput.getText().toString();
				setSummary(value+"CM");
				editor.putString("height", value);
			} else {
				String feet = feetInput.getText().toString();
				String inch = inchInput.getText().toString();
				value = ""+(Integer.valueOf(feet) * 12 + Integer.valueOf(inch));
				setSummary(MeasurementUtil.inchToTxt(Float.valueOf(value))+"Inch");
				editor.putString("inch", value);
			}
			editor.putString("measure", tb.isChecked()?"0":"1");
			editor.commit();
			
			
			
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			//change to CM
			cmInput.setVisibility(View.VISIBLE);
			inchLy.setVisibility(View.GONE);
		} else {
			//change to Inch
			cmInput.setVisibility(View.GONE);
			inchLy.setVisibility(View.VISIBLE);
		}
	}

	
}
